<?php

/**
 * This is the model class for table "{{cmp_message}}".
 *
 * The followings are the available columns in table '{{cmp_message}}':
 * @property integer $zcm_id
 * @property integer $zm_id_form
 * @property integer $zm_id_to
 * @property integer $zcm_type
 * @property string $zo_id
 * @property integer $zp_id
 * @property integer $zcm_theme
 * @property string $zcm_title
 * @property integer $zcm_content
 * @property double $zcm_offer_price
 * @property integer $zcm_offer_num
 * @property integer $zcm_reply_id
 * @property string $zcm_reply
 * @property string $zcm_name
 * @property string $zcm_tel
 * @property string $zcm_qq
 * @property string $zcm_email
 * @property string $zcm_address
 * @property integer $zcm_status
 * @property integer $inputtime
 */
class CmpMessage extends CActiveRecord
{
	//回复状态
	public $reply_status;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CmpMessage the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{cmp_message}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zcm_reply', 'required', 'on'=>'repaly'),
			array('zm_id_form, zm_id_to, zcm_type, zp_id, zcm_theme, zcm_offer_num, zcm_reply_id, zcm_status, inputtime', 'numerical', 'integerOnly'=>true),
			array('zcm_offer_price', 'numerical'),
			array('zo_id, zcm_tel', 'length', 'max'=>20),
			array('zcm_title', 'length', 'max'=>100),
			array('zcm_content', 'length', 'max'=>500),
			array('zcm_name', 'length', 'max'=>25),
			array('zcm_qq', 'length', 'max'=>15),
			array('zcm_email, zcm_address', 'length', 'max'=>255),
			array('zcm_reply', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zcm_id, zm_id_form, zm_id_to, zcm_type, zo_id, zp_id, zcm_theme, zcm_title, zcm_content, zcm_offer_price, zcm_offer_num, zcm_reply_id, zcm_reply, zcm_name, zcm_tel, zcm_qq, zcm_email, zcm_address, zcm_status, inputtime, reply_status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'product'=>array(self::BELONGS_TO, 'Product', 'zp_id'),//关联产品
		);
	}

	/**
	 * @$orderid 订单id
	 * @return 获取订单留言
	 */
	public function getOrderMessage($orderid)
	{
		$messages = $this->findAllByAttributes(array('zo_id'=>$orderid));
		return $messages;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zcm_id' => '询盘',
			'zm_id_form' => '发起人',
			'zm_id_to' => '咨询人',
			'zcm_type' => '咨询类型',
			'zo_id' => '订单号',
			'zp_id' => '产品',
			'zcm_theme' => '主题/回复',
			'zcm_title' => '标题',
			'zcm_content' => '内容',
			'zcm_offer_price' => '采购价格',
			'zcm_offer_num' => '采购数量',
			'zcm_reply_id' => '回复ID',
			'zcm_reply' => '回复内容',
			'zcm_name' => '联系人',
			'zcm_tel' => '联系电话',
			'zcm_qq' => 'QQ',
			'zcm_email' => 'EMAIL',
			'zcm_address' => '联系地址',
			'zcm_status' => '状态',
			'inputtime' => '创建时间',
			'reply_status'=>'回复状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zcm_id',$this->zcm_id);
		$criteria->compare('zm_id_form',$this->zm_id_form);
		$criteria->compare('zm_id_to',$this->zm_id_to);
		$criteria->compare('zcm_type',$this->zcm_type);
		$criteria->compare('zo_id',$this->zo_id,true);
		$criteria->compare('zp_id',$this->zp_id);
		$criteria->compare('zcm_theme',$this->zcm_theme);
		$criteria->compare('zcm_title',$this->zcm_title,true);
		$criteria->compare('zcm_content',$this->zcm_content);
		$criteria->compare('zcm_offer_price',$this->zcm_offer_price);
		$criteria->compare('zcm_offer_num',$this->zcm_offer_num);
		$criteria->compare('zcm_reply_id',$this->zcm_reply_id);
		$criteria->compare('zcm_name',$this->zcm_name,true);
		$criteria->compare('zcm_tel',$this->zcm_tel,true);
		$criteria->compare('zcm_qq',$this->zcm_qq,true);
		$criteria->compare('zcm_email',$this->zcm_email,true);
		$criteria->compare('zcm_address',$this->zcm_address,true);
		$criteria->compare('zcm_status',$this->zcm_status);
		$criteria->compare('inputtime',$this->inputtime);
		
		if(!empty($this->reply_status)){
			if($this->reply_status==1){
				$criteria->addCondition('zcm_reply =""');//回复为空
			}elseif($this->reply_status==2){
				$criteria->addCondition('zcm_reply !=""');//回复不为空
			}
		}else{
			$criteria->compare('zcm_reply',$this->zcm_reply,true);
		}

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 获取回复状态
	 */
	public function getReplyStatus($item=null){
		if($item==null){
			return array(
					'1'=>'未回复',
					'2'=>'已回复',
					);
		}else{
			$data = $this->getReplyStatus();
			if(array_key_exists($item,$data))
				return $data[$item];
		}
	}
}